/*
 * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.advance.injector.processor;

import com.baomidou.mybatisplus.advance.injector.ASTBuilder;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.List;

/**
 * 生成常量
 * 比如User有userId,userName  给User生成 User.USERID,User.USERNAME
 *
 * @author wanglei
 * @since 2022-03-14
 */
public class GenFieldsConstantProcessor implements MybatisPlusAPTProcessor {
    @Override
    public void generate(ASTBuilder astBuilder) {
        astBuilder.getFields().forEach(field -> {
            // 生成常量 比如 public static final String USERID = "userId";
            JCTree.JCVariableDecl constantField = astBuilder.getTreeMaker().VarDef(
                    astBuilder.getTreeMaker().Modifiers(Flags.PUBLIC + Flags.STATIC + Flags.FINAL, List.nil()),  //访问变量标识符
                    astBuilder.getNames().fromString(field.name.toString().toUpperCase()),        //变量名
                    astBuilder.typeRef("java.lang.String"),     //变量类型
                    astBuilder.getTreeMaker().Literal(field.name.toString())        //变量初始值
            );
            astBuilder.addField(constantField);
        });

    }
}
